#include "seqlist.h"

seqlist* seqlistCreate(int len)
{
    seqlist* p=(seqlist*)malloc(sizeof(seqlist));
    if(p==NULL){
        ERROR_INFO("malloc seqlist error");
        exit(-1);
    }
    p->data=(int*)malloc(sizeof(int)*len);
    if(p->data==NULL){
        ERROR_INFO("malloc p->date error");
        exit(-1);
    }
    p->len=len;
    p->last=-1;
    return p;
}

void seqlistFull(seqlist *p)
{
    if(p->last==p->len){
        ERROR_INFO("seqlist is full");
        exit(-1);
    }
}

void seqlistEmpty(seqlist *p)
{
    if(p->last==-1){
        ERROR_INFO("seqlist is empty");
        exit(-1);
    }
}

int seqlistInter(seqlist *p,int data)
{
    seqlistFull(p);

    p->last++;

    p->data[p->last]=data;
    return 0;
}

int seqlistDelete_post(seqlist* p,int x)
{
    seqlistEmpty(p);

    if(x>p->last){
        ERROR_INFO("delete post is invalid");
        exit(-1);
    }

    for(int i=x;i<p->last;i++){
        p->data[i]=p->data[i+1];
    }
    p->last--;
    return 0;
}

int seqlistDate_post(seqlist* p,int data)
{
    seqlistEmpty(p);

    for(int i=0;i<=p->last;i++){
        if(p->data[i]==data){
            return i;
        }
    }

    return -1;
}

int seqlistDelete_date(seqlist* p,int data)
{
   int post = seqlistDate_post(p,data);

   if(post == -1){
        ERROR_INFO("seqlist data is invalid");
        exit(-1);
   }

    for(int i=post;i<p->last;i++){
        p->data[i]=p->data[i+1];
    }
    p->last--;

    return 0;
}

int seqlistChange(seqlist* p,int x,int data)
{
    seqlistEmpty(p);
    if(x>p->last){
        ERROR_INFO("seqlist change is invalid");
        exit(-1);
    }

    p->data[x]=data;

    return 0;
}

void seqlistShow(seqlist* p)
{
    seqlistEmpty(p);

    int i=0;
    for(;i<=p->last;i++){
        printf("p->data[%d]:%d\n",i,p->data[i]);
    }
    return;
}